<template>
    <!-- 文章发布 -->
    <div class="pageWrap">
        <el-button size="mini" type="primary" @click="addArticle">添加文章</el-button>
        <el-table :data="artList" style="width: 100%">
            <el-table-column prop="title" label="标题" width="500">
            </el-table-column>
            <el-table-column prop="createTime" label="更新时间" width="180">
            </el-table-column>
            <el-table-column label="文章公告">
                <template slot-scope="scope">
                    <el-button size="mini" @click="aeNotice(scope.row)">{{scope.row.noticeId?'编辑':'添加'}}公告</el-button>
                    <el-button size="mini" type="danger" v-if="scope.row.noticeId"
                        @click="delNotice(scope.row.noticeId)">删除公告</el-button>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="downCode(scope.$index, scope.row)">下载二维码</el-button>
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="文章内容" width="90%" :visible.sync="dialogFormVisible">
            <el-form :model="articleForm" class="pageWrap" label-width="auto">
                <el-form-item label="标题" required>
                    <el-input v-model="articleForm.title" placeholder="标题" style="width:100%;float:left">
                    </el-input>
                </el-form-item>
                <el-form-item label="封面">
                    <el-upload ref="upbanner" class="avatar-uploader" :action="upLoadUrl" :show-file-list="false"
                        :on-success="(value)=> picSuccess(value)" :before-upload="(file)=> beforeUpload(file)"
                        name="img">
                        <img v-if="articleForm.pic" :src="articleForm.pic" class="avatar banner" />
                        <i v-else class="el-icon-plus avatar-uploader-icon banner"></i>
                    </el-upload>
                    <span class="tips">注：请上传jpg格式，建议尺寸400*247 长方形</span>
                </el-form-item>
                <el-form-item label="关联名称">
                    <el-input placeholder="请输入内容" v-model="articleForm.name" clearable>
                    </el-input>
                </el-form-item>
                <el-form-item label="关联赛事">
                    <el-select v-model="matchIds" multiple filterable placeholder="请选择" style="width:100%">
                        <el-option v-for="item in matchArr" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="内容" required>
                    <RichTxt ref="arteditor"></RichTxt>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="savearticle()">保存</el-button>
            </div>
        </el-dialog>
        <el-dialog title="公告设置" width="90%" :visible.sync="noticeVisible">
            <notice :isArt="true" :artId="artId" :noticeId="noticeId" @onclose="noticeVisible=false;getArtList()" />
            <!-- <div slot="footer" class="dialog-footer">
                <el-button>取 消</el-button>
                <el-button type="primary">保存</el-button>
            </div> -->
        </el-dialog>
    </div>
</template>

<script>
    import RichTxt from "@/components/editor/editor";
    import notice from "../matchNotice/matchNotice.vue"
    export default {
        components: {
            RichTxt,
            notice
        },
        data() {
            return {
                matchIds: [],
                matchArr: [],
                artList: [],
                dialogFormVisible: false,
                articleForm: {
                    id: 0,
                    title: "",
                    pic: "",
                    content: "",
                    name: '',
                    noticeId: ''
                },
                upLoadUrl: this.$url + "/sbr/match/uploadImg",
                noticeVisible: false,
                artId: '',
                noticeId: ''
            };
        },
        methods: {
            // 下载二维码
            downCode(index, row) {
                let u = "/pages/news/news?id=" + row.id
                this.$axios.post('/sbr/getwxcode', {
                    path: u
                }).then(res => {
                    let base64 = res.data.data
                    var cvs = document.createElement("canvas");
                    cvs.width = 440
                    cvs.height = 440
                    cvs.style.display = "none";
                    var ctx = cvs.getContext("2d");
                    var img = document.createElement("img");
                    img.src = "data:image/png;base64," + base64
                    img.style.display = "none";
                    document.body.appendChild(img)
                    img.onload = function () {
                        ctx.drawImage(img, 20, 20, 400, 400);
                        console.log(111);
                        const imageData = ctx.getImageData(0, 0, 440, 440);
                        for (let i = 0; i < imageData.data.length; i += 4) {
                            // 当该像素是透明的，则设置成白色
                            if (imageData.data[i + 3] === 0) {
                                imageData.data[i] = 255;
                                imageData.data[i + 1] = 255;
                                imageData.data[i + 2] = 255;
                                imageData.data[i + 3] = 255;
                            }
                        }
                        ctx.putImageData(imageData, 0, 0);
                        let url = cvs.toDataURL("image/png"); //得到图片的base64编码数据
                        let a = document.createElement("a"); // 生成一个a元素
                        let event = new MouseEvent("click"); // 创建一个单击事件
                        a.download = "文章推广码.png"; // 设置图片名称
                        a.href = url; // 将生成的URL设置为a.href属性
                        a.dispatchEvent(event);
                    }
                })
            },
            getMatch() {
                let q = {
                    name: "",
                    type: "",
                    state: "",
                    startDate: "",
                    endDate: "",
                }
                let oo = this.$Base64.encode("orgId=" + this.$orgId);
                oo = this.$Base64.encode(oo);
                this.$axios
                    .get("/sbr/match/getMatchsApp?name=&state=0&type=&place=&date=")
                    .then((res) => {
                        let rr = res.data.data;
                        this.matchArr = rr
                    });
            },
            // 编辑文章 打开弹框
            handleEdit(index, row) {
                console.log(index, row);
                this.articleForm = row
                this.matchIds = row.matchId.split(',').map(Number)
                this.dialogFormVisible = true
                setTimeout(() => {
                    this.$refs.arteditor.content = row.content
                }, 300);
            },
            // 删除文章
            handleDelete(index, row) {
                console.log(index, row);
                this.$axios.get("/sbr/match/delMatchAdvertise?id=" + row.id).then(res => {
                    this.getArtList()
                })
            },
            picSuccess(v) {
                this.articleForm.pic = v.data.filePath;
            },
            // 上传图片
            beforeUpload(file) {
                if (this.articleForm.pic) {
                    let pp = this.articleForm.pic.split("/");
                    pp = this.$Base64.encode(pp[pp.length - 1]);
                    this.$axios.get("/sbr/match/delImg?fileName=" + pp);
                }
                const isJPG = file.type === "image/jpeg";
                const isLt1M = file.size / 1024 / 1024 < 1;
                if (!isJPG) {
                    this.$message.error("上传图片只能是 JPG 格式!");
                }
                if (!isLt1M) {
                    this.$message.error("上传图片大小不能超过 1MB!");
                }
                return isJPG && isLt1M;
            },
            // 添加文章 打开弹框
            addArticle() {
                this.articleForm = {
                    id: 0,
                    title: "",
                    pic: "",
                    content: "",
                    name: '',
                    noticeId: ''
                }
                this.dialogFormVisible = true
            },
            // 保存文章
            savearticle() {
                let obj = this.articleForm
                obj.content = this.$refs.arteditor.content
                obj.matchId = this.matchIds.join(',')
                console.log(obj)
                this.$axios.post('/sbr/match/putMatchAdvertise', obj).then(res => {
                    this.getArtList()
                    this.dialogFormVisible = false
                })
            },
            // 获取文章列表
            getArtList() {
                this.$axios.get("sbr/match/selectMatchAdvertise").then(res => {
                    let rr = res.data.data
                    this.artList = rr
                })
            },
            // 添加修改 公告
            aeNotice(row) {
                console.log(row.id, row.noticeId);
                this.artId = row.id
                this.noticeId = row.noticeId
                this.noticeVisible = true;
            },
            // 删除公告
            delNotice(noticeid) {
                this.$axios
                    .get("/sbr/notice/delMatchNotice?id=" + noticeid)
                    .then((res) => {
                        this.$message.success("删除成功");
                        this.getArtList();
                    })
                    .catch((err) => {
                        this.$message.success("删除失败");
                    });
            }
        },
        mounted() {
            this.getMatch()
            this.getArtList()
        }
    }
</script>
<style scoped>
    /* @import url(); 引入公共css类 */
    .pageWrap {
        margin: 10px auto;
        width: 95%;
    }

    .btn {
        display: block;
        margin: 0 auto;
    }

    .avatar-uploader>>>.el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader>>>.el-upload:hover {
        border-color: #409eff;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 150px;
        height: 150px;
        line-height: 150px;
        text-align: center;
    }

    .avatar {
        width: 150px;
        height: 150px;
        display: block;
    }

    .tips {
        font-size: 12px;
        float: left;
        line-height: 1.2;
    }

    .avatar-uploader.banner>>>.el-upload {
        height: 123px !important;
    }

    .banner {
        width: 200px !important;
        height: 123px !important;
        line-height: 123px !important;
    }
</style>